/* 
 * File:   rstream.h
 * Author: sergey
 *
 * Created on November 20, 2011, 7:38 PM
 */

#ifndef RSTREAM_H
#define	RSTREAM_H

#include <cstdlib>
#include <math.h>

class RandomStream {
public:
    RandomStream() {
        srand(time(NULL));
        n = rand();
        b = 0;
    }
    bool nextBool() {
        if (b < 31)
            return (n & (1 << (b++))) > 0;
        n = rand();
        b = 1;
        return n & 1;
    }
    
    inline bool nextBool(const double p) {
        return nextInt() < floor(RAND_MAX * p);
    }
    
    int nextInt() {
        return rand();
    }
private:
    UInt32 n, b;
} randomStream;

#endif	/* RSTREAM_H */

